<template>
  <div class="masthead d-flex">
    <div class="masthead-cards text-center my-auto">
      <div class="mb-1" id="namen">
        <img src="@/assets/teach/tch_title.png" class="tch_title" v-if="!started" />
        <div v-if="started" class="userInfo">
          <img :src="avatar"  class="avatar ant-avatar ant-avatar-sm">
          <label class="avatar_info">{{namen}}</label>
        </div>
      </div>
      <img v-if="!started" src="@/assets/teach/tch_open.png" class="tch_btn" @click="sos" />
      <img v-if="started" src="@/assets/teach/tch_stop.png" class="tch_btn" @click="sos" />
    </div>
    <div class="overlay">
      <p id="demo"></p>
    </div>
  </div>
</template>

<script>
import {getNames,} from '@/api/apiz'

export default {
  name: '',
  components: {},
  data() {
    return {
      avatar: '',
      namen: '开始点名',
      linkText: '开始',
      started: false,
      names: [],
      classId: '',
      timer: null,
    }
  },

  methods: {

    async getNamesData(id) {
      let parms = {
        classId: id
      }
      let res = await getNames(parms);
      if (res.code == 200 || res.code == '200') {
        this.names = res.result
      }
    },
    sos() {
      if (this.started) {
        clearInterval(this.timer);
        var d = new Date();
        this.namen = this.names[d.getMilliseconds() * 101 % this.names.length].studentName;
        this.avatar = "https://guopei.bj.bcebos.com" + this.names[d.getMilliseconds() * 101 % this.names.length].avatar;
        this.linkText = "开始";
        this.started = false;
      } else {
        this.linkText = "停止";
        this.started = true;
        let l = this.names.length;
        let nameData = this.names;
        this.timer = setInterval(() => {
          let index = parseInt(Math.random() * l);
          this.namen = nameData[index].studentName;
          this.avatar = "https://guopei.bj.bcebos.com" + nameData[index].avatar;
        }, 100);
      }
    },
    changeTab() {
      this.$router.push('/teacher/random')
    }
  }
}
</script>
<style scoped lang='less'>
.masthead {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, rgba(255, 255, 255, .1) 0, rgba(255, 255, 255, .1) 100%), url("../../assets/teach/tch_bg.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  .mb-1{
    height: 120px;
    margin-bottom: 3vw;
  }
  .userInfo{
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .ant-avatar-sm {
    width: 80px;
    height: 80px;
    line-height: 24px;
    border-radius: 50%;
  }
  .avatar_info{
    font-family: Microsoft YaHei, Microsoft YaHei-Bold;
    font-size: 16px;
    color: #fff;
    font-weight: bold;
    letter-spacing: 3rpx;
    margin-top: 10px;
  }
  .masthead-cards {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: relative;
    top: -5vw;
  }
  .tch_title{
    width: 350px;
  }
  .tch_btn{
    width: 130px;
    cursor: pointer;
  }
}

</style>
